package com.meatball.authorization.config.handler

import com.alibaba.fastjson2.JSON
import com.meatball.core.config.BaseResponse
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import org.springframework.security.access.AccessDeniedException
import org.springframework.security.web.access.AccessDeniedHandler

class CustomAccessDeniedHandler : AccessDeniedHandler {
    override fun handle(
        request: HttpServletRequest?,
        response: HttpServletResponse?,
        accessDeniedException: AccessDeniedException?
    ) {
        response?.status = HttpServletResponse.SC_FORBIDDEN
        response?.contentType = "application/json;charset=UTF-8"
        response?.writer?.write(JSON.toJSONString(BaseResponse.error(HttpServletResponse.SC_FORBIDDEN, "访问被拒绝")))
//        response?.writer?.write("您没有权限操作该资源")
    }
}